package fr.SDroid.Onglet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import fr.SDroid.R;
import fr.SDroid.TranSpeed;
import fr.SDroid.Calcul.Dijkstra;
import fr.SDroid.Graph.Graph;
import fr.SDroid.Graph.Ligne;
import fr.SDroid.Graph.Station;
import fr.SDroid.Util.LigneAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.ExpandableListView.OnGroupClickListener;

public class Calcul extends Activity{
	
	private Graph graph = null;
	private Dijkstra dij = null;
	private ArrayList<HashMap<String, Object>> resultatsCoresp;
	private ArrayList<HashMap<String, Object>> resultatsTemp;
	
	public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
    	 setContentView(R.layout.calcul);

    	 graph = TranSpeed.getGraph();
  	 
    	 TextView entete  = (TextView) findViewById(R.id.entete_liste);
       	 entete.setText("De "+graph.getDepart().getName()+" à "+graph.getArrive().getName()); 
       	 startCalcul();
       	 
	}
	
	public void startCalcul() {
		final ProgressDialog pd = ProgressDialog.show(this, "Working...", "Calcul en cours", true, false);    	
    	new Thread(new Runnable() {
			
			public void run() {
				dij = new Dijkstra(graph);
		       	dij.startCalcul();
		       	pd.dismiss();
		       	handler.sendEmptyMessage(0);
			}
    	}).start();
	}
	
	private Handler handler = new Handler() {
    	public void handleMessage(android.os.Message msg) {
    		if(msg.what == 0) {
    		//	Toast.makeText(Calcul.this,dij.getDebut().getName() + " -- " + dij.getFin().getName(), 10000).show();
    			printResult(dij);
    		}    		
    	}
    };
	
	public void printResult(Dijkstra dij) {
		resultatsCoresp = createTableCoresp(dij);
		resultatsTemp = createTableTemp(dij);
		
		HashMap<String, String> group1 = new HashMap<String, String>();
		group1.put("text", "moins de corespondance");
		
		HashMap<String, String> group2 = new HashMap<String, String>();
		group2.put("text", "moins de temps");
		
		ArrayList<HashMap<String, String>> groupData = new ArrayList<HashMap<String, String>>();
		groupData.add(group1);
		groupData.add(group2);
		
		ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();
		childData.add(resultatsCoresp);
		childData.add(resultatsTemp);
		
		
		
		SimpleExpandableListAdapter adapt = new LigneAdapter(
				this,
				groupData, 
				R.layout.itemgroupe, 
				new String[] {"text"}, 
				new int[] {R.id.detailLigne},
				childData, 
				R.layout.itemligne, 
				new String[] {"image", "text"}, 
				new int[] { R.id.imageViewLigne, R.id.detailLigne});
		
		final ExpandableListView elv = (ExpandableListView) findViewById(R.id.ListViewResultCoresp);
		elv.setAdapter(adapt);
		
		elv.setOnGroupClickListener(new OnGroupClickListener() {
			
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
//				AlertDialog.Builder adb = new AlertDialog.Builder(Calcul.this);
//				adb.setTitle("titreeee");
//				adb.setPositiveButton("Ok", null);
        		//on affiche la boite de dialogue
//        		adb.show();
				return false;
			}
		});
/*		SimpleAdapter adapter1 = new SimpleAdapter(this,
				resultatsCoresp,
				R.layout.itemligne,
				new String[] {"text"}, 
	            new int[] {R.id.detailLigne});
		final ListView lvc = (ListView)findViewById(R.id.ListViewResultCoresp);
		lvc.setAdapter(adapter1);
		
		resultatsTemp = createTableTemp(dij);
		SimpleAdapter adapter2 = new SimpleAdapter(this,
				resultatsTemp,
				R.layout.itemligne,
				new String[] {"text"}, 
	            new int[] {R.id.detailLigne});
		final ListView lvt = (ListView)findViewById(R.id.ListViewResultTemp);
		lvt.setAdapter(adapter2);*/
		
	/*	lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				HashMap<String, String> map = (HashMap<String, String>) lv.getItemAtPosition(position);
				
				AlertDialog.Builder adb = new AlertDialog.Builder(Calcul.this);
				adb.setTitle(map.get("title"));
				String affiche = "";
				for(int i = 0; i < resultats.size(); i++) {
					
					if(resultats.get(i).get("title").equals(map.get("title"))) {
						ArrayList<Station> listStation = (ArrayList<Station>) resultats.get(i).get("list");
						for(int j = 0; j < listStation.size(); j++) {
							affiche = affiche + "-" +listStation.get(j).getName();
						}
					}
				}
				adb.setMessage(affiche);
				adb.setPositiveButton("Ok", null);
        		//on affiche la boite de dialogue
        		adb.show();
				
				
			}
		});*/
	}
	
	private ArrayList<HashMap<String, Object>> createTableCoresp(Dijkstra dij) {
		ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
		ArrayList<HashMap<Ligne, ArrayList<Station>>> result = dij.getResultCoresp();
		
		for(int i = 0; i < result.size(); i++) {
				for(Entry<Ligne, ArrayList<Station>> entry : result.get(i).entrySet()) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("image",entry.getKey().getImage());
					map.put("text", entry.getValue().get(entry.getValue().size()-1).getName() + " --> " + entry.getValue().get(0).getName());
					items.add(0,map);
				}
		}
        return items;
	}
	
	private ArrayList<HashMap<String, Object>> createTableTemp(Dijkstra dij) {
		ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
		ArrayList<HashMap<Ligne, ArrayList<Station>>> result = dij.getResultTemp();
		
		for(int i = 0; i < result.size(); i++) {
				for(Entry<Ligne, ArrayList<Station>> entry : result.get(i).entrySet()) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("image", entry.getKey().getImage());
					map.put("text", entry.getValue().get(entry.getValue().size()-1).getName() + " --> " + entry.getValue().get(0).getName());
					items.add(0,map);
				}
		}
        return items;
	}

}
